package algorithm.questions;

import java.util.Arrays;

/**
 * @author DengZhiYuan
 * @version 1.0
 * @date 2021/12/28 18:45
 */
public class Questions2021 {

    public static void main(String[] args) {
        new Questions2021().study1228();
    }

    /**
     * 今天华为机考遇到了一道题，输入一个列表，求列表内三个元素之和的最大值，且这个最大值不能超过100。各位有啥思路，我机考的时候暴力解法用时过长没通过。
     */
    private void study1228(){
        int[] arrays = {2,3,33,97,98,150,33,34};
        int num = 100;
        int res = 0;
        Arrays.sort(arrays);
        outer:
        for (int i = 0; i <(arrays.length -2) ; i++) {
            int begin = i+1;
            int end = arrays.length-1;
            while (begin < end){
                int cur = arrays[i]+arrays[begin]+arrays[end];
                if(cur == num){
                    res = cur;
                    break outer;
                }
                if(cur < num){
                    res = Math.max(cur, res);
                    begin++;
                }
                if(cur > num){
                    end --;
                }
            }
        }
        System.out.println(res);
    }





}
